/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.servlet;

import acceso.util.UserSesion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.directwebremoting.Container;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;

/**
 *
 * @author john
 */
public class PushSesion extends HttpServlet {
    
    public void init(ServletConfig config) throws ServletException{
        Container container = ServerContextFactory.get().getContainer();
        ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
        ScriptSessionListener listener = new ScriptSessionListener() {
            public void sessionCreated(ScriptSessionEvent ev) {
                HttpSession session = WebContextFactory.get().getSession();
                UserSesion objSesion = null;
                objSesion = (UserSesion) session.getAttribute("sesion");
                System.out.println("user id: "+objSesion.getCo_usuari());
                ev.getSession().setAttribute("co_usuari", objSesion.getCo_usuari());
            }

            public void sessionDestroyed(ScriptSessionEvent sse) {
                
                System.out.println("Se cerro el scriptsession: "+sse.getSession().getAttribute("co_usuari"));
            }
        };
        manager.addScriptSessionListener(listener);
        super.init(config);
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Container container = ServerContextFactory.get().getContainer();
        ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
        Collection<ScriptSession> scripts = manager.getAllScriptSessions();
        
        PrintWriter out = response.getWriter();
         try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Numero de paginas : " + scripts.size() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    
    
}
